#include<stdio.h>
#include<stdlib.h>

#include "util.h"
#include "myutil.h"

#define EXIT(){\
	fprintf(stdout, "Left-value error.\n");\
	exit(1);\
}

A_var transVar(myVarList list){
	A_var result = NULL;
	struct myVar cur;

	while(list){
		cur = *list;
		if(cur.mode == GET_SIMPLE)
			result = A_SimpleVar(cur.pos, S_Symbol(cur.field.id));	
		else if(cur.mode == GET_FIELD)
			if(result != NULL)
				result = A_FieldVar(cur.pos, result, S_Symbol(cur.field.id));
			else
				EXIT()
		else
			if(result != NULL)
				result = A_SubscriptVar(cur.pos, result, cur.field.idx);
			else
				EXIT()
			
		list = list->next;
	}

	return result;
}

myVarList getVarList(struct myVar var){
	myVarList head = (myVarList)checked_malloc(sizeof(struct myVar));
	*head = var;
	return head;
}
